package net.jueb.frameWorkDemo.mvc4mod.controler;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.concurrent.Callable;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.web.context.request.async.WebAsyncTask;
import com.google.gson.Gson;

public class AbstractBaseController {

	protected Logger log=LoggerFactory.getLogger(getClass());
	/**
	 * 回复字节数据
	 * @param response
	 * @param data
	 */
	protected void response(HttpServletResponse response,byte[] data)throws Exception
	{
		if(data!=null)
		{
			response.getOutputStream().write(data);
			response.getOutputStream().flush();
			response.getOutputStream().close();
		}
	}
	
	/**
	 * @param in
	 * @return
	 */
	@Deprecated
	public byte[] readAllBytes(InputStream in)throws Exception
	{
		byte[] data=new byte[]{};
		if(in!=null)
		{
			ByteArrayOutputStream outSteam = new ByteArrayOutputStream();  
	        byte[] buffer = new byte[1024];  
	        int len = -1;  
	        while ((len = in.read(buffer)) != -1) {  
	            outSteam.write(buffer, 0, len);  
	        }  
	        outSteam.close();  
	        data=outSteam.toByteArray();  
		}
		return data;
	}
	
	public byte[] readAllBytesAndClose(InputStream in)throws Exception
	{
		byte[] data=readAllBytes(in);
		in.close();
		return data;
	}
	
	/**
	 * 直接返回,不阻塞
	 * @param result
	 * @return
	 */
	protected <T> WebAsyncTask<T> buildTask(final T result)
	{
		Callable<T> call=new Callable<T>() {
			@Override
			public T call() throws Exception {
				return result;
			}
		};
		return buildTask(call);
	}
	
	protected WebAsyncTask<Void> buildVoidTask()
	{
		Callable<Void> call=new Callable<Void>() {
			@Override
			public Void call() throws Exception {
				return null;
			}
		};
		return buildTask(call);
	}
	
	/**
	 * 阻塞方法在Callable的call方法中
	 * @param async
	 * @return
	 */
	protected <T> WebAsyncTask<T> buildTask(final Callable<T> async)
	{
		return new WebAsyncTask<T>(async);
	}
	
	protected <T> void responseJson(HttpServletResponse response,T result)
	{
		if(result!=null)
		{
			Gson gson=new Gson();
	    	String json=gson.toJson(result);
	    	response.setContentType("application/json");
	    	try {
	    		response.getOutputStream().write(json.getBytes("UTF-8"));
				response.getOutputStream().flush();
				response.getOutputStream().close();
			} catch (Exception e) {
				log.error(e.getMessage(), e);
			}
		}
	}
	
	protected <T> boolean responseJson_old(HttpServletResponse response,T result)
	{
		if(response==null || result==null)
		{
			return false;
		}
		MappingJackson2HttpMessageConverter c=new MappingJackson2HttpMessageConverter();
    	try {
			c.write(result, MediaType.APPLICATION_JSON, new ServletServerHttpResponse(response));
			return true;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
    	return false;
	}
}
